﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for VisitPopup.xaml
    /// </summary>
    public partial class VisitPopup : Window
    {
        public VisitPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            visit = new DTCVisit();
            MainGrid.DataContext = visit;
            DoctorsListBox.ItemsSource = Doctors;
            PatientsListBox.ItemsSource = Patients;
        }

        private DTCVisit visit;
        private List<DTCDoctor> doctors;
        private List<DTCPatient> patients;
        private bool isNew=true;

        public List<DTCDoctor> Doctors
        {
            get
            {
                if (doctors == null)
                    doctors = App.DBI.GetAllDoctors();
                return doctors;
            }
        }
        public List<DTCPatient> Patients
        {
            get
            {
                if (patients == null)
                    patients = App.DBI.GetAllPatiens();
                return patients;
            }
        }
        public DTCDoctor SelectedDoctor
        {
            get
            {
                return (DTCDoctor) DoctorsListBox.SelectedItem;
            }
            set
            {
                DoctorsListBox.SelectedItem = value;
            }
        }
        public DTCPatient SelectedPatient
        {
            get
            {
                return (DTCPatient)PatientsListBox.SelectedItem;
            }
            set
            {
                PatientsListBox.SelectedItem = value;
            }
        }

        public DTCVisit Visit
        {
            get
            {
                return visit;
            }
            set
            {
                if (visit != value)
                {
                    visit = value;
                    isNew = false;
                    MainGrid.DataContext = visit;
                    SelectedDoctor = visit.DTCDoctor;
                    SelectedPatient = visit.DTCPatient;
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (DialogResult == true)
            {
                visit.DTCDoctor = (DTCDoctor)DoctorsListBox.SelectedItem;
                visit.DTCPatient = (DTCPatient)PatientsListBox.SelectedItem;
                if (isNew)
                {
                    App.DBI.Db.DTCVisits.InsertOnSubmit(visit);
                }
                if (visit.Description != Description.Text)
                    visit.Description = Description.Text;
                App.DBI.Db.SubmitChanges();
            }
            else if (!isNew)
            {
                App.DBI.Db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, visit);
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PrepareBindings(MainGrid);
        }

        private void PrepareBindings(FrameworkElement Container)
        {
            BindingExpression expression;
            TextBox tmp;
            foreach (object child in LogicalTreeHelper.GetChildren(Container))
            {
                if (child is TextBox)
                {
                    tmp = child as TextBox;
                    expression = tmp.GetBindingExpression(TextBox.TextProperty);
                    if (expression != null)
                    {
                        expression.UpdateSource();
                    }
                }
                else
                {
                    if (child is FrameworkElement)
                        PrepareBindings(child as FrameworkElement);
                }
            }
        }
    }
}
